Java - How to load JAR files dynamically at Runtime?
To load JAR files dynamically at runtime in Java, you can use the URLClassLoader class. Here's a basic example demonstrating how to do this:

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

public class DynamicJarLoader {

    public static void main(String[] args) throws Exception {
        // Specify the path to the JAR file
        String jarFilePath = "path/to/your/jar/file.jar";

        // Create a File object representing the JAR file
        File jarFile = new File(jarFilePath);

        // Convert the file path to a URL
        URL jarUrl = jarFile.toURI().toURL();

        // Create a URLClassLoader with the JAR file URL
        URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl});

        // Load a class from the JAR file
        Class<?> loadedClass = classLoader.loadClass("com.example.MyClass");

        // Create an instance of the loaded class using reflection
        Object instance = loadedClass.getDeclaredConstructor().newInstance();

        // Call a method on the loaded class instance
        loadedClass.getMethod("someMethod").invoke(instance);

        // Close the class loader to release resources
        classLoader.close();
    }
}
In this example:

Replace "path/to/your/jar/file.jar" with the actual path to your JAR file.
We create a File object representing the JAR file and convert its path to a URL.
Using the URL, we create a URLClassLoader instance, passing the JAR file URL as an array.
We load a class from the JAR file using loadClass().
We create an instance of the loaded class using reflection and call a method on it.
Finally, we close the class loader to release resources.
Keep in mind the following considerations:

Ensure that the JAR file is present at the specified path.
Handle exceptions appropriately, such as ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, and InvocationTargetException.
Be cautious when dynamically loading classes at runtime, as it can lead to security issues if not done carefully.
Make sure the classes you are dynamically loading are compatible with the rest of your application.
Examples
How to dynamically load JAR files in Java at runtime?

Description: This query seeks methods to dynamically load JAR files during runtime in Java, enabling flexibility and extensibility in the application.
// Java code to dynamically load JAR file at runtime
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
Runtime loading of JAR files in Java with reflection

Description: Demonstrates the use of reflection to dynamically load classes from JAR files at runtime in Java.
// Java code for runtime loading of JAR file with reflection
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = Class.forName("fully.qualified.ClassName", true, classLoader);
Dynamically add JAR files to Java classpath at runtime

Description: Explains how to add JAR files to the Java classpath dynamically during runtime, enabling dynamic class loading.
// Java code for dynamically adding JAR files to classpath at runtime
URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrlMethod.setAccessible(true);
addUrlMethod.invoke(classLoader, new File("path/to/jarFile.jar").toURI().toURL());
How to load external JAR files dynamically in Java application?

Description: Discusses methods to load external JAR files dynamically within a Java application at runtime.
// Java code to load external JAR file dynamically in Java application
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
Dynamic loading of JAR files in Java using custom class loader

Description: Illustrates the implementation of a custom class loader to dynamically load JAR files in Java applications.
// Java code for dynamic loading of JAR file using custom class loader
CustomClassLoader classLoader = new CustomClassLoader();
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName", "path/to/jarFile.jar");
How to load JAR files dynamically from a specific directory in Java?

Description: Provides techniques for dynamically loading JAR files from a specific directory during runtime in Java.
// Java code to dynamically load JAR files from a specific directory
File jarDir = new File("directory/path");
URL[] urls = {jarDir.toURI().toURL()};
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
Dynamic class loading of JAR files in Java application

Description: Describes the process of dynamically loading classes from JAR files within a Java application at runtime.
// Java code for dynamic class loading of JAR files in Java application
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
How to load classes from JAR files dynamically without restarting Java application?

Description: Discusses methods to load classes from JAR files dynamically without the need to restart the Java application.
// Java code to load classes from JAR files dynamically without restarting application
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
Load JAR files dynamically and execute methods in Java

Description: Demonstrates loading JAR files dynamically in Java and executing methods from the loaded classes.
// Java code to load JAR files dynamically and execute methods
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("path/to/jarFile.jar").toURI().toURL()});
Class<?> loadedClass = classLoader.loadClass("fully.qualified.ClassName");
Object instance = loadedClass.newInstance();
Method method = loadedClass.getMethod("methodName");
method.invoke(instance);
Dynamic JAR file loading in Java with ServiceLoader

Description: Utilizes Java's ServiceLoader mechanism for dynamically loading services from JAR files at runtime.
// Java code for dynamic JAR file loading with ServiceLoader
ServiceLoader<ServiceInterface> loader = ServiceLoader.load(ServiceInterface.class);
for (ServiceInterface service : loader) {
    service.method();
}